package com.xudm.cdg.config;

import com.xudm.cdg.annotation.CurrentVisitor;
import com.xudm.cdg.interceptor.LoginUtil;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * 当前访客参数解析器
 *
 * @Author xudm
 */
public class CurrentVisitorArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        //只有参数类型是 String，并且该参数被注解CurrentVisitor所标识，才进行下一步处理
        return methodParameter.getParameterType().isAssignableFrom(String.class) && methodParameter.hasParameterAnnotation(CurrentVisitor.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        String loginId = LoginUtil.getLoginId();
        if (loginId == null) {
            throw new RuntimeException("获取用户信息失败,请重新登录");
        }
        return loginId;
    }
}
